package guda.talk.web.test;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.webapp.WebAppContext;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;

/**
 * Created by well on 15/3/13.
 */
public class JettyServer {

    //jetty容器监听的端口，可以随意修改
    private int jettyPort = 8087;
    //auto-config默认配置文件在~/下面
    private String autoConfigName = "app-local.properties";
    //webapp目录，同时用来定位web.xml，是相对项目主控pom的目录
    private String webappDir = "talk-web/src/main/webapp";

    public static void main(String[] args) throws Exception {
        JettyServer jetty = new JettyServer();
        jetty.start();
    }

    public void start() throws Exception {
        loadPropertiesFromClasspath();
        Server server = new Server(jettyPort);
        HandlerCollection collection = new HandlerCollection();

        ResourceHandler resourceHandler = new ResourceHandler();
        resourceHandler.setDirectoriesListed(true);
        System.out.println(System.getProperty("user.dir") + File.separatorChar + "assets" + File.separator+"app");
        resourceHandler.setResourceBase(System.getProperty("user.dir") + File.separatorChar + "assets" + File.separator+"app");
        collection.addHandler(resourceHandler);

        collection.addHandler(createWebapp());

        server.setHandler(collection);
        server.start();
        server.join();
    }

    private void loadProperties() {
        File file = new File(System.getProperty("user.home") + File.separatorChar + autoConfigName);
        if (file.canRead()) {
            try {
                System.out.println("=====环境变量========");
                System.getProperties().load(new FileInputStream(file));
                Set<Object> objects = System.getProperties().keySet();
                List keyList = new ArrayList();
                keyList.addAll(objects);
                Collections.sort(keyList);
                Iterator iterator = keyList.iterator();
                while (iterator.hasNext()) {
                    Object key = iterator.next();
                    System.out.println(key + "=" + System.getProperties().get(key));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    private void loadPropertiesFromClasspath() {
        InputStream resourceAsStream = this.getClass().getResourceAsStream("/"+autoConfigName);
        //File file = new File(System.getProperty("user.home") + File.separatorChar + autoConfigName);

        try {
            System.out.println("=====环境变量========");
            System.getProperties().load(resourceAsStream);
            Set<Object> objects = System.getProperties().keySet();
            List keyList = new ArrayList();
            keyList.addAll(objects);
            Collections.sort(keyList);
            Iterator iterator = keyList.iterator();
            while (iterator.hasNext()) {
                Object key = iterator.next();
                System.out.println(key + "=" + System.getProperties().get(key));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    protected WebAppContext createWebapp() {
        WebAppContext webapp = new WebAppContext();
        webapp.setDescriptor(getWebDescriptor());
        webapp.setResourceBase(getAppRoot() + File.separatorChar + webappDir);
        webapp.setContextPath("/");
        webapp.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false");
        webapp.setInitParameter("org.eclipse.jetty.servlet.Default.useFileMappedBuffer", "false");
        return webapp;
    }


    private String getWebDescriptor() {
        return getAppRoot() + File.separatorChar + webappDir + File.separatorChar + "WEB-INF" + File.separatorChar + "web.xml";
    }


    protected String getAppRoot() {
        return System.getProperty("user.dir");
    }


}
